/*
* Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description: Bookmarkview.qml.
*/

import QtQuick 1.0
import QtMobility.location 1.2

/*!
 *  \file
 *  \brief Defines Bookmark view to browse and share locations.
 *
 *  Bookmark view consists of two button elements and a ListView element that
 *  uses LandmarkModel to show the stored landmarks. It uses "connectionEngine"
 *  context properties to start a connection to other device and the context properties
 *  of "locationEngine" to send tapped location to other device.
 */

Rectangle {
    id: myBookmarkView
    anchors.fill: parent
    color: "gray"
    opacity: 0.8

    // The model
    LandmarkModel {
        id: myLandmarkModel
        sortBy: LandmarkModel.NameSort;
        sortOrder: LandmarkModel.AscendingOrder
    }

    // The view
    ListView {
        id: myLandmarkListView
        model: myLandmarkModel
        delegate: myLandmarkListDelegate
        width: parent.width;
        height: parent.height
        highlightFollowsCurrentItem: false
        focus: true
        anchors.fill: parent
        keyNavigationWraps: true
    }

    // The delegate
    Component {
        id: myLandmarkListDelegate

        Item {
            width: 200; height: 50
            Text {
                id: nameField;
                text: landmark.name
                font.pixelSize: 25
                anchors.verticalCenter: parent.verticalCenter
                x: 5
            }

            MouseArea {
                anchors.fill: parent;
                onClicked: {
                    clickEffect.play
                    if (sharing == false) {
                        // Notify Map to update to a new location coordinates.
                        topMostView.posChange = myLandmarkModel.landmarks[index].coordinate
                        // Notify Map Text to update landmark name.
                        locationManager.locationName = myLandmarkModel.landmarks[index].name
                    } else {
                        // Start sharing the location with another device.
                        locationManager.shareCurrentLocation(myLandmarkModel.landmarks[index].name, myLandmarkModel.landmarks[index].coordinate.latitude, myLandmarkModel.landmarks[index].coordinate.longitude)
                    }
                }
            }
        }
    }

    Rectangle {
        width: parent.width
        height: 60
        y: parent.height - height
        color:  "gray"
        opacity: 0.8

        Button {
            x: 5
            label: topMostView.sharing == false ? "Connect" : "Disconnect";
            onButtonClicked: {
                // Start sharing a location with another device.
                clickEffect.play
                if (topMostView.sharing == false) {
                    connectionEngine.openConnection()
                    topMostView.sharing = true;
                } else {
                    connectionEngine.closeConnection()
                    topMostView.sharing = false;
                }
            }
        }

        Button {
            x: parent.width / 2 + 5
            label: "Close"
            onButtonClicked: {
                // Close bookmark view.
                clickEffect.play
                myBookmarkView.visible = false;
                topMostView.sharing = false;
            }
        }
    }
}
